#region Copyright & License
// Copyright 2008-2009 BUGCOLLECT.COM
// Author: Remus Rusanu
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//                 http://bugcollect.com/terms
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Diagnostics;

namespace bugcollect.open
{
    internal class ReportSubmissionState
    {
        #region Fields
        private object _userState;
        private Guid _reportUuid;
        private ReportSubmissionResultHandler _userCallback;

        private byte[] _reportHash;
        private MemoryStream _streamReport;
        private HttpWebRequest _request;
        private int _transferOffset;
        private Stream _requestStream;
        // the size of the buffer is designed to cover a typical SSL frame
        private byte[] _buffer = new byte[16384 - 5 - 16];
        #endregion

        #region Properties

        public Guid ReportUuid
        {
            get { return _reportUuid; }
        }

        public object UserState
        {
            get { return _userState; }
            set { _userState = value; }
        }

        public ReportSubmissionResultHandler UserCallback
        {
            get { return _userCallback; }
            set { _userCallback = value; }
        }

        public byte[] ReportHash
        {
            get { return _reportHash; }
        }

        public HttpWebRequest Request
        {
            get { return _request; }
            set { _request = value; }
        }

        public long Size
        {
            get { return _streamReport.Length; }
        }

        public int TransferOffset
        {
            get { return _transferOffset; }
            set { _transferOffset = value; }
        }

        public Stream ReportStream
        {
            get { return _streamReport; }
        }

        public Stream RequestStream
        {
            get { return _requestStream; }
            set { _requestStream = value; }
        }

        public byte[] Buffer
        {
            get { return _buffer; }
        }
        #endregion

        #region Callback Handling
        internal void PerformCallback(
            ReportingManager manager,
            Result result,
            Exception ex)
        {
            try
            {
                if (null != _userCallback)
                {
                    ReportSubmissionResultArgs args =
                        new ReportSubmissionResultArgs(_userState, result, ex);
                    _userCallback(manager, args);
                }
            }
            catch (Exception exDispatch)
            {
                Debug.WriteLine(exDispatch);
            }
        }
        #endregion

        #region Initialization

        public ReportSubmissionState(
            Report report,
            object userState,
            ReportSubmissionResultHandler callback)
        {
            _reportUuid = report.UUID;
            _userState = userState;
            _userCallback = callback;

            _streamReport = new MemoryStream(report.EstimateSize());
            report.WriteRequestBody(_streamReport);
            _streamReport.Seek(0, SeekOrigin.Begin);

            _reportHash = report.ComputeHash();
        }

        #endregion

    }
}
